/** 
 * 2011-10-10 上午11:51:07 
 * Client.java 
 * author:sunb 
 */
package com.u6.udp.test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lamfire.utils.Bytes;
import com.u6.udp.netty.UdpMessage;
import com.u6.udp.netty.UdpMessageEncoder;

/**
 * @author sunbin
 * 
 */
public class Client2 implements Runnable {

	public static void main(String[] args) {

		ExecutorService executorService = Executors.newCachedThreadPool();
		for (int i = 0; i < 1; i++) {
			executorService.execute(new Client2());
		}

	}

	public void run() {
		// TODO Auto-generated method stub
		try {
			//InetSocketAddress clientAddress = new InetSocketAddress("192.168.0.143", 9100);

			DatagramSocket datagramSocket = new DatagramSocket();

			short messageID = 4;
			short type = 1;
			long sendUid = 12345;
			long receiveUid = 45678;
			byte[]	data = Bytes.toBytes("张三");
			int   length = data.length;
			UdpMessage message = new UdpMessage(messageID, type, sendUid, receiveUid, length, data);

			byte[] bs = UdpMessageEncoder.encode(message);

			InetSocketAddress serverAddress = new InetSocketAddress("192.168.0.143", 8080);
			DatagramPacket sendPacket = new DatagramPacket(bs, bs.length, serverAddress);
			datagramSocket.send(sendPacket);

			while(true){
			
				byte[] rs = new byte[2];
				
		
				DatagramPacket revicePack = new DatagramPacket(rs, rs.length);
				datagramSocket.receive(revicePack);
				short messageid=Bytes.toShort(rs);
				System.out.println("revice:" + messageid);
				
				if(messageid==5){
					
				}
			}

			//datagramSocket.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
